<template>
  <div class="login">
    <!-- 头像区域 -->
    <div class="touxiang"><img src="~assets/img/logo.png" alt=""></div>
    
    <div class="form">
      <el-form ref="loginForm" :model="formInfo" :rules="loginRules">
        <el-form-item  prop="username">
          <el-input placeholder="请填写您的账号" v-model="formInfo.username">
            <template #prefix>
              <i class="iconfont icon-user"></i>
            </template>
          </el-input>
        </el-form-item> 

        <el-form-item prop="password">
          <el-input placeholder="请填写您的密码" v-model="formInfo.password" type="password">
            <template #prefix>
              <i class="iconfont icon-3702mima"></i>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="loginFormReset">重置</el-button>
        </el-form-item>

      </el-form>
    </div>
  </div>
  
</template>

<script>
export default {
  name: "Login",
  data(){
    return {
      formInfo: {
        username: "admin",
        password: "123456",
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入用户密码', trigger: 'blur' },
          { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    loginFormReset(){
      this.$refs.loginForm.resetFields();
    },
    login(){
      this.$refs.loginForm.validate(async (valid)=>{
        if( !valid ) return;
        // this.$http.post("/login",this.formInfo).then(res=>{
        //   console.log(res.data);
        // }).catch(err=>{
        //   console.log(err);
        // })
        const res = (await this.$http.post("/login",this.formInfo)).data;
        // console.log(res);
        if(res.meta.status !== 200) return this.$message.error("登陆失败");
        this.$message.success("登陆成功");
        // 将token值保存到客户端session中

        window.sessionStorage.setItem("token",res.data.token);
        this.$router.push("/home");
      })
    }
  }
}
</script>


<style scoped>
  .login{
    width: 500px;
    height: 300px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    background-color: #fff;
    border: 5px solid #f6f6f6;
    border-radius: 10px;
  }

  .touxiang{
    width: 130px;
    height: 130px;
    border: 2px solid #f6f6f6;
    border-radius: 50%;
    padding: 10px;
    position: absolute;
    left: 50%;
    transform: translate(-50%,-50%);
    background-color: #fff;
  }
    
  .login img{
    width: 130px;
    height: 130px;
    border-radius: 50%;
    background-color: #f6f6f6;
  }

  .form{
    width: 100%;
    height: 100%;
    padding: 100px 30px 0px;
    box-sizing: border-box;
  }

  .btns{
    position: absolute;
    right: 30px;
  }
  
</style>